home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / SceneFont.cpp < prev    next >
C/C++ Source or Header  |  2005-11-30  |  5KB  |  181 lines

  1.  
  2. #include "FileLogger.h"
  3. #include "EngineCore.h"
  4. #include "SceneFont.h"
  5.  
  6. namespace peon
  7. {
  8.     SceneFont::SceneFont()
  9.     {
  10.         m_r = m_g = m_b = m_a = 1.0f;
  11.         m_char_width = 16;
  12.         m_char_height = 16;
  13.         m_char_spacing = 14;
  14.  
  15.         m_fxCount = 16;
  16.         m_fyCount = 16;
  17.  
  18.         m_font_batching = false;
  19.  
  20.  
  21.     }
  22.  
  23.     SceneFont::~SceneFont()
  24.     {
  25.         unloadFont();
  26.     }
  27.  
  28.     bool SceneFont::loadFont(int char_width, int char_height, int char_spacing)
  29.     {
  30.  
  31.  
  32.         int     loop;
  33.         float    cx;                                                                // Holds Our X Character Coord
  34.         float    cy;                                                                // Holds Our Y Character Coord
  35.         float   cwx;                                                            // CharWidth in texture units
  36.         float   cwy;
  37.         
  38.         m_char_width = char_width;
  39.         m_char_height = char_height;
  40.         m_char_spacing = char_spacing;                                                            // CharHeight in texture units
  41.  
  42.         cwx         = (1.0f / 256.0f) * m_char_width;
  43.         cwy         = (1.0f / 256.0f) * m_char_height;
  44.         
  45.         m_display_list = glGenLists(m_fxCount * m_fyCount);                                    // Creating Display Lists
  46.             
  47.         for (loop=0; loop<(m_fxCount * m_fyCount); loop++)                            // Loop Through All Lists
  48.         {
  49.             cx=float(loop%m_fxCount) * cwx;                                        // X Position Of Current Character
  50.             cy=float(loop/m_fyCount) * cwy;                                        // Y Position Of Current Character
  51.  
  52.             glNewList(m_display_list + loop,GL_COMPILE);                                    // Start Building A List
  53.                 glBegin(GL_QUADS);                                                // Use A Quad For Each Character
  54.                     glTexCoord2f(cx,1-cy-cwy);                                    // Texture Coord (Bottom Left)
  55.                     glVertex2i(0, m_char_height);                                            // Vertex Coord (Bottom Left)
  56.                     glTexCoord2f(cx+cwx,1-cy-cwy);                                // Texture Coord (Bottom Right)
  57.                     //glVertex2i(m_char_width - 1,0);                                    // Vertex Coord (Bottom Right)
  58.                     glVertex2i( m_char_width, m_char_height);
  59.                     glTexCoord2f(cx+cwx,1-cy);                                    // Texture Coord (Top Right)
  60.                     //glVertex2i(m_char_width - 1,m_char_height -1);                            // Vertex Coord (Top Right)
  61.                     glVertex2i( m_char_width, 0);
  62.                     glTexCoord2f(cx,1-cy);                                        // Texture Coord (Top Left)
  63.                     //glVertex2i(0,m_char_height -1);                                    // Vertex Coord (Top Left)
  64.                     glVertex2i(0, 0);
  65.                 glEnd();                                                        // Done Building Our Quad (Character)
  66.                 glTranslated(m_char_spacing,0,0);                                        // Move To The Right Of The Character
  67.             glEndList();                                                        // Done Building The Display List
  68.         }
  69.  
  70.         return true;
  71.     }
  72.  
  73.     void SceneFont::unloadFont()
  74.     {
  75.         glDeleteLists(m_display_list,m_fxCount * m_fyCount);
  76.     }
  77.  
  78.     void SceneFont::renderText(float xpos, float ypos, const String& strText)
  79.     {
  80.         
  81.         int width = EngineCore::getSingleton().getRenderer()->getWidth();
  82.         int height= EngineCore::getSingleton().getRenderer()->getHeight();
  83.         
  84.         if(!m_font_batching)
  85.         {
  86.         
  87.             glEnable(GL_BLEND);
  88.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  89.             glDisable(GL_DEPTH_TEST);
  90.  
  91.             glDisable(GL_LIGHTING);
  92.         
  93.                                                         // Disables Depth Testing
  94.             glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
  95.             glPushMatrix();                                                            // Store The Projection Matrix
  96.             glLoadIdentity();                                                        // Reset The Projection Matrix
  97.             glOrtho(0,width,height,0,-1,1);                                        // Set Up An Ortho Screen
  98.             glMatrixMode(GL_MODELVIEW);                                                // Select The Modelview Matrix
  99.         
  100.         }
  101.         
  102.         glPushMatrix();                                                            // Store The Modelview Matrix
  103.         glLoadIdentity();                                                        // Reset The Modelview Matrix
  104.         glTranslatef(xpos,ypos,0.0f);                                            // Position The Text (0,0 - Bottom Left)
  105.         glColor4f(m_r, m_g, m_b, m_a);
  106.         glListBase( m_display_list - 32 + 128 );
  107.         glCallLists((int)strText.length(), GL_UNSIGNED_BYTE, strText.c_str());                                // Write The Text To The Screen
  108.         
  109.         if( !m_font_batching )
  110.         {
  111.         
  112.             glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
  113.             glPopMatrix();                                                            // Restore The Old Projection Matrix
  114.             glMatrixMode(GL_MODELVIEW);                                                // Select The Modelview Matrix
  115.             glPopMatrix();                                                            // Restore The Old Projection Matrix
  116.         
  117.             glEnable(GL_LIGHTING);
  118.  
  119.             glDisable(GL_BLEND);
  120.             glEnable(GL_DEPTH_TEST);
  121.         }
  122.  
  123.  
  124.     }
  125.  
  126.     void SceneFont::setColor(float r, float g, float b, float a)
  127.     {
  128.         m_r = r;
  129.         m_g = g;
  130.         m_b = b;
  131.         m_a = a;
  132.     }
  133.  
  134.     bool SceneFont::beginBatchFont()
  135.     {
  136.         
  137.         
  138.         int width = EngineCore::getSingleton().getRenderer()->getWidth();
  139.         int height= EngineCore::getSingleton().getRenderer()->getHeight();
  140.     
  141.         m_font_batching = true;
  142.  
  143.         glEnable(GL_BLEND);
  144.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  145.         glDisable(GL_DEPTH_TEST);
  146.  
  147.         glDisable(GL_LIGHTING);
  148.     
  149.                                                     // Disables Depth Testing
  150.         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
  151.         glPushMatrix();                                                            // Store The Projection Matrix
  152.         glLoadIdentity();                                                        // Reset The Projection Matrix
  153.         glOrtho(0,width,height,0,-1,1);                                        // Set Up An Ortho Screen
  154.         glMatrixMode(GL_MODELVIEW);                                                // Select The Modelview Matrix
  155.  
  156.  
  157.         return true;
  158.     }
  159.  
  160.     void SceneFont::endBatchFont()
  161.     {
  162.  
  163.         m_font_batching = false;
  164.  
  165.         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
  166.         glPopMatrix();                                                            // Restore The Old Projection Matrix
  167.         glMatrixMode(GL_MODELVIEW);                                                // Select The Modelview Matrix
  168.         glPopMatrix();                                                            // Restore The Old Projection Matrix
  169.     
  170.         glEnable(GL_LIGHTING);
  171.  
  172.         glDisable(GL_BLEND);
  173.         glEnable(GL_DEPTH_TEST);
  174.  
  175.  
  176.     }
  177.  
  178.  
  179. }
  180.  
  181.